// 路由文件
import { createRouter, createWebHistory } from "vue-router";
import { menuStore } from '../utils/store';


import Login from '../views/Login.vue'
import Home from '../views/Home.vue'

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home,
        children:[
            {
                path: '/index',
                name: 'Index',
                component: () => import('../views/Index.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/note',
                name: 'Note',
                component: () => import('../views/Note.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/resources',
                name: 'Resources',
                component: () => import('../views/Resources.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/userManager',
                name: 'UserManager',
                component: () => import('../views/manager/UserManager.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/roleManager',
                name: 'RoleManager',
                component: () => import('../views/manager/RoleManager.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/authorManager',
                name: 'AuthorManager',
                component: () => import('../views/manager/AuthorManager.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/about',
                name: 'About',
                component: () => import('../views/About.vue'),
            },
            {
                path: '/threeD',
                name: 'ThreeD',
                component: () => import('../views/ThreeD.vue'),
            },
            {
                path: '/userCenter',
                name: 'UserCenter',
                component: () => import('../views/UserCenter.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/menuManager',
                name: 'MenuManager',
                component: () => import('../views/manager/MenuManager.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/message',
                name: 'Message',
                component: () => import('../views/Message.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/',
                name: 'Blog',
                component: () => import('../views/Blog.vue'),
            },
            {
                path: '/photos',
                name: 'Photos',
                component: () => import('../views/Photos.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/article/:id',
                props: true,
                name: 'Article',
                component: () => import('../views/Article.vue'),
            },
            {
                path: '/tagManager',
                name: 'TagManager',
                component: () => import('../views/manager/NoteTagManager.vue'),
            },
            {
                path: '/chatGpt',
                name: 'ChatGpt',
                component: () => import('../views/ChatGpt.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/systemLog',
                name: 'SystemLog',
                component: () => import('../views/manager/SystemLog.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/noteManager',
                name: 'NoteManager',
                component: () => import('../views/manager/NoteManager.vue'),
                meta:{
                    requireAuth: true
                },
            },
            {
                path: '/403',
                name: '403',
                component: () => import('../views/403.vue'),
                meta: {
                    title: '页面丢失'
                },
            },
            // {
            //     path: '/404',
            //     name: '404',
            //     component: () => import('../views/404.vue')
            // }
            { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('../views/404.vue') }
            
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: Login
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})


router.beforeEach((to,from)=>{
    if(to.meta.requireAuth) {
        let token = localStorage.getItem('token');
        let loginStatus = localStorage.getItem('loginStatus')
        if(!token||loginStatus=='no'){
            return {
                path: '/login'
            }
        }
    }
})

export default router;
